package com.topisv.tms.service;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.topisv.tms.entity.Function;
import com.topisv.tms.entity.Performance;
import com.topisv.tms.entity.QueryParamHolder;
import com.topisv.tms.repository.FunctionDao;
import com.topisv.tms.repository.PerformanceDao;
import com.topisv.tms.repository.RolepermDao;
import com.topisv.tms.repository.UserroleDao;
import com.topisv.tms.service.base.BaseService;

//Spring Bean的标识.
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional(readOnly = true)
public class PerformanceService extends BaseService{

	@Autowired
	private PerformanceDao performanceDao;
	
	@Autowired
	private JdbcTemplate jdbcTemplate;

	public Performance getPerformance(Long id) {
		return performanceDao.findOne(id);
	}

	@Transactional(readOnly = false)
	public void savePerformance(Performance entity) {
		performanceDao.save(entity);
	}

	@Transactional(readOnly = false)
	public void deletPeformance(Long id) {
		performanceDao.delete(id);
	}

	public List<Performance> getAllPerformance() {
		return (List<Performance>) performanceDao.findAll();
	}
	
	public Page<Performance> getPerformance(QueryParamHolder paramHolder) {
		return findByJson(paramHolder, performanceDao, Performance.class);
	}
	
	/**
	   * 
	   * @param startTime
	   * @param endTime
	   * @param customerCode
	   * @return
	   */
	  public List<Map<String, Object>> getCount(String startTime, String endTime,String wcode) {
	    StringBuilder sb = new StringBuilder();
	    sb.append("SELECT warehouse_code,customer_code, realname,optype,sum(qty) as qty FROM tt_performance where ");
	    sb.append(" warehouse_code=? and work_time BETWEEN ? AND ?  GROUP BY warehouse_code,customer_code,realname,optype");
	    Object[] params = null;
	    params = new Object[] {wcode, startTime, endTime};
	    return jdbcTemplate.queryForList(sb.toString(), params);
	  }
	
}
